package com.heima.wemedia.service.service.impl;

import com.github.pagehelper.PageHelper;
import com.heima.file.MinIOTemplate;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dto.WmMaterialDto;
import com.heima.model.wemedia.entity.WmMaterial;
import com.heima.wemedia.service.dao.WmMaterialDao;
import com.heima.wemedia.service.service.WmMaterialService;
import com.heima.wemedia.service.utils.WmThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * 自媒体素材表(WmMaterial)表服务实现类
 *
 * @author makejava
 * @since 2022-04-10 21:06:24
 */
@Service("wmMaterialService")
public class WmMaterialServiceImpl implements WmMaterialService {
    @Autowired
    private WmMaterialDao wmMaterialDao;

    @Autowired
    private MinIOTemplate minIOTemplate;


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public WmMaterial queryById(Long id) {
        return this.wmMaterialDao.queryById(id);
    }


    /**
     * 新增数据
     *
     * @param wmMaterial 实例对象
     * @return 实例对象
     */
    @Override
    public WmMaterial insert(WmMaterial wmMaterial) {
        this.wmMaterialDao.insert(wmMaterial);
        return wmMaterial;
    }

    /**
     * 修改数据
     *
     * @param wmMaterial 实例对象
     * @return 实例对象
     */
    @Override
    public WmMaterial update(WmMaterial wmMaterial) {
        this.wmMaterialDao.update(wmMaterial);
        return this.queryById(wmMaterial.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.wmMaterialDao.deleteById(id) > 0;
    }

    @Override
    public ResponseResult upload(MultipartFile multipartFile) {
        //效验参数
        if (multipartFile == null || multipartFile.getSize() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //上传图片
        String filename = multipartFile.getOriginalFilename();
        String suffix = filename.substring(filename.lastIndexOf("."));
         filename = UUID.randomUUID().toString().replace("-", "") + suffix;
        String url = null;
        try {
            url = minIOTemplate.uploadImgFile("",filename,multipartFile.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"素材上传失败");
        }
        //保存素材数据到数据库
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(Long.valueOf(WmThreadLocalUtil.getUser().getId()));
        wmMaterial.setUrl(url);
        wmMaterial.setType(0);
        wmMaterial.setIsCollection("0");
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setUpdatedTime(new Date());

        wmMaterialDao.insert(wmMaterial);
        //封装数据返回
        return ResponseResult.okResult(wmMaterial);
    }


    @Override
    public ResponseResult collect(Long id) {
        return null;
    }

    @Override
    public ResponseResult cancel(Long id) {
        return null;
    }

    @Override
    public ResponseResult queryByPage(WmMaterialDto dto) {
        //效验参数
        dto.checkParam();
        //封装查询条件
        PageHelper.startPage(dto.getPage(),dto.getSize());
        WmMaterial queryParam = new WmMaterial();
        if (dto.getIsCollection() != null) {
            queryParam.setIsCollection(dto.getIsCollection() + "");
        }
        Page<WmMaterial> page = wmMaterialDao.queryAll(queryParam);

        //ResponseResult result= new PageResponseResult(page.getPageNum,page.getPaeSize(),(int)page.getTotal());
        PageResponseResult result = new PageResponseResult(page.getPageNum(), page.getPageSize(), (int) page.getTotal());
        result.setData(page.getResult());
        return result;
    }


}
